package com.linxianqin.springbootVue.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.convert.SysNoticeConvert;
import com.linxianqin.springbootVue.domain.dto.SysNoticeDTO;
import com.linxianqin.springbootVue.domain.dto.SysRoleDTO;
import com.linxianqin.springbootVue.domain.entity.SysNotice;
import com.linxianqin.springbootVue.domain.enums.BusinessType;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.page.TableDataInfo;
import com.linxianqin.springbootVue.domain.query.SysNoticeQuery;
import com.linxianqin.springbootVue.manage.annotation.SysLog;
import com.linxianqin.springbootVue.manage.core.BaseController;
import com.linxianqin.springbootVue.service.SysNoticeService;
import com.linxianqin.springbootVue.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lxq
 * @version 1.0
 * @description:  公告 信息操作处理
 * @date 2020/12/28 15:49
 */
@Slf4j
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController  extends BaseController {
    private final SysNoticeService sysNoticeService;
    public SysNoticeController(SysNoticeService sysNoticeService){
        this.sysNoticeService=sysNoticeService;
    }

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNoticeQuery query)
    {
        log.info("查询的信息：{}",query);
        //构建分页bean
        Page<SysNoticeDTO> page = buildPage();
        //查询分页数据
        Page<SysNoticeDTO> dtoPage = sysNoticeService.selectRolePage(page, query);
        return getDataTable(dtoPage);
    }


    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return AjaxResult.success(sysNoticeService.getById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @SysLog(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNoticeDTO noticeDTO)
    {
        noticeDTO.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysNoticeService.save(SysNoticeConvert.INSTANCE.dto2entity(noticeDTO)));
    }


    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @SysLog(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNoticeDTO noticeDTO)
    {
        noticeDTO.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysNoticeService.updateById(SysNoticeConvert.INSTANCE.dto2entity(noticeDTO)));
    }
    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @SysLog(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable List<Long> noticeIds)
    {
        return toAjax(sysNoticeService.removeByIds(noticeIds));
    }
}
